package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Currency;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: CurrencyDB.java,v 1.1 2004/10/24 23:37:11 mihaipostelnicu Exp $
 */
public class CurrencyDB extends DBPersistenceManager
{

      private CurrencyDB(PersistenceToken token) {
            super(token);
      }

      public static CurrencyDB getManager(PersistenceToken token) {
            return new CurrencyDB(token);
      }

      public final Currency[] getAll()
            throws SQLException
      {
            Connection con = db_token.getCFService().getConnection(
                  db_token.getPool_name());

            Currency[] result = null;
            ArrayList items = new ArrayList();

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_ALL );
                  ResultSet rs = ps.executeQuery();

                  while ( rs.next() )
                  {
                        Currency item = new Currency();
                        item.setIso3( rs.getString( 1 ) );
                        item.setName( rs.getString( 2 ) );

                        items.add( item );
                  }

                  rs.close();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            if ( items.size() > 0 )
            {
                  result = new Currency[ items.size() ];
                  items.toArray( result );
            }

            return result;
      }

      private static final String GET_ALL =
            "SELECT iso3, name FROM currencies ORDER BY iso3 ASC";
}
